#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2025/1/11
# @Author : Joker
# @Version : Python 3.7
# @Software: PyCharm
import yaml
from utils.path import EXTRACT_FILE


class YamlUtil:

    @staticmethod
    def get_from_yaml(key=None, yaml_path=None):
        yaml_path = EXTRACT_FILE if yaml_path is None else yaml_path

        with open(yaml_path, encoding="utf-8") as f:
            value = yaml.safe_load(f)

        return value.get(key) if key else value

    @staticmethod
    def write_to_yaml(data, yaml_path=None):
        yaml_path = EXTRACT_FILE if yaml_path is None else yaml_path

        with open(yaml_path, encoding="utf-8", mode="a+") as f:
            yaml.safe_dump(data, f, allow_unicode=True)

    @staticmethod
    def clean_yaml(yaml_path=None):
        yaml_path = EXTRACT_FILE if yaml_path is None else yaml_path

        with open(yaml_path, encoding="utf-8", mode="w"):
            pass

    @staticmethod
    def get_case_from_yaml(yaml_path):
        with open(yaml_path, encoding="utf-8") as f:
            case_list = yaml.safe_load(f)

        if len(case_list) >= 2:
            for case in case_list:
                if case.get('step'):
                    return [case_list]
        return case_list


yamlUtil = YamlUtil()
